/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 *
 * 			http://www.kkkd.com/
 *
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseActivity;
import com.ouertech.android.agm.lib.ui.base.BaseFragment;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.Tag;
import com.simon.mengkou.data.bean.base.Topic;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.bean.req.ShareOrderLikeReq;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.VideoListActivity;
import com.simon.mengkou.ui.fragment.HomeFollowFragment;
import com.simon.mengkou.ui.fragment.HomeFragment;
import com.simon.mengkou.ui.fragment.UserVideoFragment;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.CollectionButton;
import com.simon.mengkou.ui.view.FollowButton;
import com.simon.mengkou.ui.view.SmallFollowButton;
import com.simon.mengkou.ui.view.flowlayout.FlowLayout;
import com.simon.mengkou.utils.UtilOuer;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/14.
 * desc    : 首页精选视频适配器
 */
public class HomeVideoAdapter extends AbsAdapter<Follow> {
    private HomeFragment mActivity;

    public HomeVideoAdapter(Context context, HomeFragment fragment) {
        super(context);
        mActivity = fragment;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_user_video_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Follow data = getItem(position);

        final User user = data.getUser();
        //头像
        holder.mSdvAvatar.loadAvatar(user.getAvatarUrl(), user.getLevel());
        holder.mSdvAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开用户详情
                OuerDispatcher.presentUserActivity(mContext, user.getUserId(), user.getNick());
            }
        });
        //昵称
        holder.mTvName.setText(user.getNick());
        //时间
        holder.mTvTime.setText(UtilOuer.getFormatDate(mContext, data.getCreateAt(), "yy-M-d"));
        //关注
//        if(data.isFollowed()) {
//            holder.mFbFollow.setVisibility(View.GONE);
//        } else {
//            holder.mFbFollow.follow(false);
//            holder.mFbFollow.setVisibility(View.VISIBLE);
//            holder.mFbFollow.setOnClickListener(new View.OnClickListener(){
//
//                @Override
//                public void onClick(View v) {
//                    if(!data.isFollowed()) {
//                        data.setFollowed(true);
//                        holder.mFbFollow.setVisibility(View.GONE);
//                        OuerApplication.mUser.setTopicNum(OuerApplication.mUser.getTopicNum() + 1);
//                        AgnettyFuture future = OuerApplication.mOuerFuture.videoFollow(data.getId(), null);
//                        mActivity.attachDestroyFutures(future);
//
//                        //发送视频收藏广播
//                        OuerDispatcher.sendVideoFollowBroadcast(mContext, data.getId());
//                    }
//                }
//            });
//        }

        holder.mFbFollow.follow(user.getInterested());
        holder.mFbFollow.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                user.setInterested(!user.getInterested());
                holder.mFbFollow.follow(user.getInterested());

                if(user.getInterested()) {
                    OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() + 1);
                } else {
                    OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() - 1);
                }

                AgnettyFuture future = OuerApplication.mOuerFuture.follow(
                        OuerApplication.mUser.getUserId(), user.getUserId(), user.getInterested(), null);
                mActivity.attachDestroyFutures(future);

                //发送关注or取消关注广播
                OuerDispatcher.sendFollowBroadcast(mContext, user.getUserId(),
                        user.getInterested());
            }
        });


        //图片
        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, data.getVideoCover());
        holder.mSdvImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开晒单详情界面
                OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
                data.setReadNum(data.getReadNum() + 1);
                holder.mTvComment.setText(UtilOuer.formatNum(mContext, data.getReadNum()));
            }
        });

//        holder.mIvVideo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //视频播放
//                OuerDispatcher.presentVideoPlayerActivity(mContext, data.getId(), data.getVideoUrl());
//            }
//        });

        //内容
        holder.mTvContent.setText(data.getContent());
        //点赞
        holder.mTVLike.setText(UtilOuer.formatNum(mContext, data.getFaverNum()));
        if(data.isLiked()) {//喜欢
            holder.mTVLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_like, 0, 0, 0);
        } else {//不喜欢
            holder.mTVLike.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_ic_unlike, 0, 0, 0);
        }

        holder.mRlLikeRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                data.setLiked(!data.isLiked());
                AgnettyFuture future = OuerApplication.mOuerFuture.shareOrderLike(data.getId(),
                        data.isLiked(), null);
                mActivity.attachDestroyFutures(future);

                ShareOrderLikeReq req = new ShareOrderLikeReq();
                req.setId(data.getId());
                req.setCheck(data.isLiked());
                OuerDispatcher.sendShareOrderLikeBroadcast(mContext, req);
            }
        });

        //评论
        holder.mTvComment.setText(UtilOuer.formatNum(mContext, data.getReadNum()));
//        holder.mRlCommentRoot.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
//            }
//        });

        //分享
        holder.mRlShareRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                mActivity.showShareDialog(data);
            }
        });

        //标签
        holder.mFlTags.removeAllViews();
        List<Tag> tags = data.getTags();
        if(UtilList.isNotEmpty(tags)) {
            holder.mFlTags.setVisibility(View.VISIBLE);
            for(final Tag tag : tags) {
                TextView tvTag = new TextView(mContext);
                tvTag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                tvTag.setBackgroundResource(R.drawable.share_order_ic_tag);
                tvTag.setSingleLine(true);
                tvTag.setEllipsize(TextUtils.TruncateAt.END);
                tvTag.setText(tag.getName());
                holder.mFlTags.addView(tvTag);

                tvTag.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        //打开标签聚合界面
                        OuerDispatcher.presentTagPostsActivity(mContext, tag.getId(), tag.getName());
                    }
                });
            }
        } else {
            holder.mFlTags.setVisibility(View.GONE);
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //头像
        @Bind(R.id.follow_id_avatar)
        AvatarSimpleDraweeView mSdvAvatar;
        //昵称
        @Bind(R.id.follow_id_name)
        TextView mTvName;
        //时间
        @Bind(R.id.follow_id_time)
        TextView mTvTime;
        //关注
        @Bind(R.id.follow_id_btn)
        FollowButton mFbFollow;
        //图片
        @Bind(R.id.follow_id_image)
        SimpleDraweeView mSdvImage;
        //图片
        @Bind(R.id.follow_id_video)
        ImageView mIvVideo;
        //内容
        @Bind(R.id.follow_id_content)
        TextView mTvContent;
        //点赞
        @Bind(R.id.follow_id_like)
        TextView mTVLike;
        //评论
        @Bind(R.id.follow_id_comment)
        TextView mTvComment;
        //
        @Bind(R.id.follow_id_like_root)
        RelativeLayout mRlLikeRoot;
        //
        @Bind(R.id.follow_id_comment_root)
        RelativeLayout mRlCommentRoot;
        //
        @Bind(R.id.follow_id_share_root)
        RelativeLayout mRlShareRoot;
        //标签
        @Bind(R.id.follow_id_tags)
        FlowLayout mFlTags;
    }
}
